import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:school_assistant/data/goods_moni.dart';
import 'package:school_assistant/post_page.dart';
import 'package:school_assistant/shiwu_page.dart';
import 'package:school_assistant/shop_page.dart';
import 'package:sp_util/sp_util.dart';

import 'common_client.dart';
import 'common_colors.dart';
import 'data/uer_info_entity.dart';
import 'hall_page.dart';
import 'keep_alive_wrapper.dart';
import 'mine_page.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _selectedIndex = 0;

  PageController pageController = PageController(
    initialPage: 0,
    keepPage: true,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: pageController,
        children: [
          KeepAliveWrapper(
            keepAlive: true,
            child: HallPage(),
          ),
          KeepAliveWrapper(
            keepAlive: true,
            child: ShopPage(),
          ),
          KeepAliveWrapper(
            keepAlive: true,
            child: ShiwuPage(),
          ),
          KeepAliveWrapper(
            keepAlive: true,
            child: Minepage(),
          ),
        ],
        onPageChanged: (index) {
          setState(() {
            _selectedIndex = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        // 底部导航
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '主页'),
          BottomNavigationBarItem(
              icon: Icon(Icons.rice_bowl_sharp), label: '美味食堂'),
          BottomNavigationBarItem(
              icon: Icon(Icons.developer_board_sharp), label: '寻物/招领'),
          BottomNavigationBarItem(icon: Icon(Icons.settings), label: '我的'),
        ],
        currentIndex: _selectedIndex,
        fixedColor: CommonColor.colorMainGreen,
        unselectedItemColor: CommonColor.color33,
        // type: BottomNavigationBarType.fixed,

        onTap: _onItemTapped,
      ),
      floatingActionButton: _selectedIndex == 0
          ? FloatingActionButton(
              backgroundColor: CommonColor.colorMainGreen,
              //悬浮按钮
              onPressed: _onAdd, //悬浮按钮
              child: const Icon(Icons.add))
          : null,
    );
  }

  @override
  void initState() {
    super.initState();
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)
      ..maskType = EasyLoadingMaskType.black
      ..loadingStyle = EasyLoadingStyle.light
      ..userInteractions = false;
    _getUser();
    if (SpUtil.getString("isFirst") == null ||
        SpUtil.getString("isFirst")?.isEmpty == true) {
      Moni.addMoni();
      SpUtil.putString("isFirst", "isFirst");
    }
  }

  _getUser() async {
    try {
      var res = await CommonClient().getUser(SpUtil.getString("uid") ?? '');
      List<dynamic> data = jsonDecode(res.data ?? "");
      if (data != null) {
        var uerInfo = UerInfoEntity.fromJson(data[0]);
        SpUtil.putString("username", uerInfo.username ?? "");
        SpUtil.putString("profile", uerInfo.profile ?? "");
        SpUtil.putInt("sex", uerInfo.sex ?? 1);
      }
    } catch (e) {
      print(e);
    }
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
      pageController.jumpToPage(
        index,
      );
    });
  }

  void _onAdd() {
    Get.to(PostPage());
  }
}
